package com.ruoyi.business.controller;

import com.alibaba.fastjson.JSON;
import com.ruoyi.business.domain.DefinitionNode;
import com.ruoyi.business.service.IDefinitionNodeService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 流程定义节点信息Controller
 * 
 * @author wolfcode
 * @date 2021-07-20
 */
@Controller
@RequestMapping("/business/definitionNode")
public class DefinitionNodeController extends BaseController
{
    private String prefix = "business/definitionNode";

    @Autowired
    private IDefinitionNodeService definitionNodeService;
    @Autowired
    private ISysUserService sysUserService;
    
    @RequiresPermissions("business:definitionNode:view")
    @GetMapping("/{definitionInfoId}")
    public String definitionNode(@PathVariable("definitionInfoId")Long definitionInfoId,ModelMap modelMap)
    {
        modelMap.put("definitionInfoId",definitionInfoId);
        return prefix + "/definitionNode";
    }

    /**
     * 查询流程定义节点信息列表
     */
    @RequiresPermissions("business:definitionNode:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DefinitionNode definitionNode)
    {
        startPage();
        List<DefinitionNode> list = definitionNodeService.selectDefinitionNodeList(definitionNode);
        return getDataTable(list);
    }

    /**
     * 新增流程定义节点信息
     */
    @GetMapping("/add")
    public String add(Long definitionInfoId,ModelMap mmap)
    {
        List<SysUser> auditors = sysUserService.selectUserList(new SysUser());
        mmap.put("auditors",auditors);
        mmap.put("definitionInfoId",definitionInfoId);
        return prefix + "/add";
    }

    /**
     * 新增保存流程定义节点信息
     */
    @RequiresPermissions("business:definitionNode:add")
    @Log(title = "流程定义节点信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DefinitionNode definitionNode)
    {
        return toAjax(definitionNodeService.insertDefinitionNode(definitionNode));
    }

    /**
     * 修改流程定义节点信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        DefinitionNode definitionNode = definitionNodeService.selectDefinitionNodeById(id);
        mmap.put("definitionNode", definitionNode);

        List<SysUser> auditors = sysUserService.selectUserList(new SysUser());
        mmap.put("auditors",auditors);

        List<Long> selectedAuditorIds = definitionNodeService.querySelectedAuditorIdsByNodeId(id);
        //注意要转成JSON字符串.
        mmap.put("selectedAuditorIds", JSON.toJSONString(selectedAuditorIds));
        return prefix + "/edit";
    }

    /**
     * 修改保存流程定义节点信息
     */
    @RequiresPermissions("business:definitionNode:edit")
    @Log(title = "流程定义节点信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DefinitionNode definitionNode)
    {
        return toAjax(definitionNodeService.updateDefinitionNode(definitionNode));
    }

    /**
     * 删除流程定义节点信息
     */
    @RequiresPermissions("business:definitionNode:remove")
    @Log(title = "流程定义节点信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(definitionNodeService.deleteDefinitionNodeByIds(ids));
    }
}
